
package ch.hearc.meteo.imp.afficheur.real.moo;

import java.awt.Color;
import java.rmi.RemoteException;
import java.util.LinkedList;
import java.util.List;

import ch.hearc.meteo.imp.afficheur.real.JFrameStationMeteo;
import ch.hearc.meteo.imp.afficheur.real.vue.atome.JPanelStationMeteoCarteElement;
import ch.hearc.meteo.imp.afficheur.real.vue.atome.JPanelStationMeteoListElement;
import ch.hearc.meteo.imp.afficheur.simulateur.moo.Stat;
import ch.hearc.meteo.imp.use.remote.PCLocal;
import ch.hearc.meteo.spec.afficheur.AffichageOptions;
import ch.hearc.meteo.spec.afficheur.AfficheurService_I;
import ch.hearc.meteo.spec.meteo.MeteoServiceOptions;
import ch.hearc.meteo.spec.meteo.listener.event.MeteoEvent;
import ch.hearc.meteo.spec.reseau.MeteoServiceWrapper_I;

public class StationMeteoElement
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public StationMeteoElement(AffichageOptions affichageOptions, MeteoServiceWrapper_I meteoService)
		{
		this.meteoService = meteoService;
		this.affichageOptions = affichageOptions;
		panelStationMeteoCarteElement = new JPanelStationMeteoCarteElement(this);
		panelStationMeteoListElement = new JPanelStationMeteoListElement(this);

		//Tools
		listAltitude = new LinkedList<MeteoEvent>();
		listPression = new LinkedList<MeteoEvent>();
		listTemperature = new LinkedList<MeteoEvent>();

		statAltitude = new Stat();
		statPression = new Stat();
		statTemperature = new Stat();
		}

	public StationMeteoElement(AffichageOptions affichageOptions, MeteoServiceWrapper_I meteoService, PCLocal pcLocal)
		{
		this(affichageOptions, meteoService);
		this.pcLocal = pcLocal;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	public void printAltitude(MeteoEvent event)
		{
		if (!isPause)
			{
			manage(listAltitude, event);
			statAltitude.update(event.getValue());
			this.panelStationMeteoCarteElement.repaint();
			JFrameStationMeteo.getInstance().getPanelInformationsMeteo().meteoDataChaned();
			}
		}

	public void printPression(MeteoEvent event)
		{
		if (!isPause)
			{
			manage(listPression, event);
			statPression.update(event.getValue());
			this.panelStationMeteoCarteElement.repaint();
			JFrameStationMeteo.getInstance().getPanelInformationsMeteo().meteoDataChaned();

			}
		}

	public void printTemperature(MeteoEvent event)
		{
		if (!isPause)
			{
			manage(listTemperature, event);
			statTemperature.update(event.getValue());
			this.panelStationMeteoCarteElement.repaint();
			JFrameStationMeteo.getInstance().getPanelInformationsMeteo().meteoDataChaned();

			}
		}

	public void refresh()
		{
		panelStationMeteoCarteElement.refresh();
		panelStationMeteoListElement.refresh();
		}

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	public void setPcLocal(PCLocal pcLocal)
		{
		this.pcLocal = pcLocal;
		}

	public void setAfficheurService(AfficheurService_I afficheurService)
		{
		this.afficheurService = afficheurService;
		}

	public void setGraphColor(Color c)
		{
		this.graphColor = c;
		}

	public void setIsPaused(boolean isPaused)
		{
		this.isPause = isPaused;
		}

	public void setMeteoServiceOptions(MeteoServiceOptions meteoServiceOptions)
		{
		try
			{
			this.meteoService.setMeteoServiceOptions(meteoServiceOptions);
			}
		catch (Exception e)
			{

			}
		}

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	public PCLocal getPcLocal()
		{
		return this.pcLocal;
		}

	public Color getGraphColor()
		{
		return this.graphColor;
		}

	public MeteoServiceWrapper_I getMeteoService()
		{
		return this.meteoService;
		}

	public MeteoServiceOptions getMeteoServiceOptions()
		{
		try
			{
			return this.meteoService.getMeteoServiceOptions();
			}
		catch (RemoteException e)
			{
			//TODO
			return null;
			}
		}

	public JPanelStationMeteoCarteElement getPanelStationMeteoCarteElement()
		{
		return this.panelStationMeteoCarteElement;
		}

	public JPanelStationMeteoListElement getPanelStationMeteoListElement()
		{
		return this.panelStationMeteoListElement;
		}

	public AfficheurService_I getAfficheurService()
		{
		return this.afficheurService;
		}

	public AffichageOptions getAffichageOption()
		{
		return this.affichageOptions;
		}

	public List<MeteoEvent> getListAltitude()
		{
		return this.listAltitude;
		}

	public synchronized List<MeteoEvent> getListPression()
		{
		return this.listPression;
		}

	public List<MeteoEvent> getListTemperature()
		{
		return this.listTemperature;
		}

	public Stat getStatAltitude()
		{
		return this.statAltitude;
		}

	public Stat getStatPression()
		{
		return this.statPression;
		}

	public Stat getStatTemperature()
		{
		return this.statTemperature;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	private void manage(List<MeteoEvent> listMeteoEvent, MeteoEvent event)
		{
		synchronized (listMeteoEvent)
			{
			if (listMeteoEvent.size() == affichageOptions.getN())
				{
				listMeteoEvent.remove(0);
				}
			listMeteoEvent.add(event);
			}

		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	//Input
	private MeteoServiceWrapper_I meteoService;
	private AfficheurService_I afficheurService;
	private PCLocal pcLocal;

	//Output
	private AffichageOptions affichageOptions;
	private Stat statAltitude;
	private Stat statPression;
	private Stat statTemperature;

	private boolean isPause;

	private Color graphColor;

	// Tools
	private List<MeteoEvent> listAltitude;
	private List<MeteoEvent> listPression;
	private List<MeteoEvent> listTemperature;
	private JPanelStationMeteoCarteElement panelStationMeteoCarteElement;
	private JPanelStationMeteoListElement panelStationMeteoListElement;

	// Outputs

	}
